package com.dhnsoft.server;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @ClassName StartServer
 * @Author dhn
 * @date 2020.11.26 08:10
 */
public class StartServer implements Runnable{
    //定义一个静态变量
    private static int ID = 100;
    public static final int TCP_PORT = 8888;
    public  String keyCode="38";
    public static List<Integer> list;

    public static int getList() {
        if(list.size()==0){
            return 0;
        }else {
            return list.get(list.size());
        }
    }

    public void setList(List<Integer> list) {
        this.list = list;
    }
    @Override
    public void run() {
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(TCP_PORT);

        } catch (IOException e) {
            e.printStackTrace();
        }
            Socket s = null;
            try {
                list = new ArrayList<>();
                s=serverSocket.accept();
                DataInputStream dis = new DataInputStream(s.getInputStream());
                String IP=s.getInetAddress().getHostAddress();
                keyCode = dis.readUTF();
                list.add(Integer.parseInt(keyCode));
                System.out.println(keyCode);
                if(list.size()==0){
                }else {
                    System.out.println("list:"+list.get(list.size()-1));
                    DataOutputStream dos = new DataOutputStream(s.getOutputStream());
                    dos.writeUTF(String.valueOf(list.get(list.size()-1)));
                }

                //读取客户端发送的请求
                //int udpPort =dis.readInt();
                //Client c = new Client(IP,udpPort);
                //clients.add(c);
                //DataOutputStream dos = new DataOutputStream(s.getOutputStream());
                //dos.writeInt(ID++);
                //System.out.println("A Client Connect ! Addr- "+s.getInetAddress() + ":" + s.getPort()+"----UDP PORT"+udpPort);
                //System.out.println("A Client Connect ! Addr- "+s.getInetAddress() + ":" + s.getPort()+"----UDP PORT");
            }catch (Exception e){
                e.printStackTrace();
            }finally {
//                if(s!=null){
//                    try {
//                        s.close();
//                        s=null;
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }
//                }
            }


    }
}
